package lian_xi;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    private int count = 3;
    private Lock lock = new ReentrantLock();
    public  void sale(){
        this.lock.lock();
        try{
            if(count>0){
                TimeUnit.SECONDS.sleep(1);
                System.out.println("【" + Thread.currentThread().getName()+"】正在卖第"+count+"张票");
            }else {
                System.out.println("【" + Thread.currentThread().getName()+"】票已卖完");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }

}

public class Reentreantlock {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                ticket.sale();
            },"售票员 - " + i).start();
        }
    }
}
